//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

// #include <sys/socket.h> //socket()、sockaddr、sendto()、recvfrom()
#include <stdio.h>  //（perror()）、printf()
#include <stdlib.h> //exit()
// #include <netinet/in.h> //sockaddr_in、（htons()）
#include <string.h>    //bzero()、strncpy()
#include <arpa/inet.h> //inet_pton()
#include <unistd.h>     //close()
#include <stdint.h>
#include "rudp.h"
#include "comm.h"

// macros are defined in comm.h
// global variables (defined in comm.c)
extern Rudp rudp;
extern int sock_fd;
extern struct sockaddr_in serv_addr; // server socket struct
extern char msg_send[g_buff_size];   // msg buffer sent to server
extern char msg_recv[g_buff_size];   // msg buffer received from server

int main(int argc, char *argv[])
{
    
    Rudp *U = RudpNew(&rudp, 1, 5);

    // test data
    // In our case, client send t1-t4 & receive r1-r4
    char t1[] = { 1, 2, 3, 4 };
    char t2[] = { 5, 6, 7, 8 };
    char t3[] = {
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
        2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 10, 11, 12, 13,
    };
    char t4[] = { 4, 3, 2, 1 };

    // init socket
    socket_init(CALL_BY_CLIENT, g_serv_port);

    // send t1-t4

    // send twice, then update twice
    RudpSend(U, t1, sizeof(t1));
    RudpSend(U, t2, sizeof(t2));
    udp_send(RudpUpdate(U, NULL, 0, 1), CALL_BY_CLIENT);
    udp_send(RudpUpdate(U, NULL, 0, 1), CALL_BY_CLIENT);
    // send twice, update once
    RudpSend(U, t3, sizeof(t3));
    udp_send(RudpUpdate(U, NULL, 0, 1), CALL_BY_CLIENT);
    RudpSend(U, t4, sizeof(t4));
    udp_send(RudpUpdate(U, NULL, 0, 1), CALL_BY_CLIENT);

    // receive r1-r4
    RudpUpdate(U, msg_recv, (int)udp_recv(CALL_BY_CLIENT), 1);
    dump_recv(U);
    RudpUpdate(U, msg_recv, (int)udp_recv(CALL_BY_CLIENT), 1);
    dump_recv(U);
    // set the tick of 3rd message to 0, do not dump_recv()
    RudpUpdate(U, msg_recv, (int)udp_recv(CALL_BY_CLIENT), 0);
    RudpUpdate(U, msg_recv, (int)udp_recv(CALL_BY_CLIENT), 1);
    dump_recv(U);

    // end
    socket_close();
    RudpDelete(U);

    return 0;
}
